package main

import (
	"flag"
	"fmt"
	xray "gitee.com/gam6it/SecToolkit/chaitin/xray"
	"log"
	"time"
)

func main() {
	var u, ei, token, ip string
	var projectId, planId int
	flag.StringVar(&u, "url", "", "Xray URL地址")
	flag.StringVar(&ei, "ei", "", "引擎ID")
	flag.StringVar(&token, "token", "", "Token")
	flag.IntVar(&projectId, "project", 1, "Project ID")
	flag.IntVar(&planId, "plan", 1, "Plan ID")
	flag.StringVar(&ip, "ip", "", "IP地址")
	flag.Parse()
	if ei == "" || u == "" || token == "" || ip == "" {
		log.Fatalf("输入不能为空！\n")
	}
	client := xray.NewClient(u, token, projectId, []string{ei}, 5*time.Minute)
	plan, err := client.Plan(planId)
	if err != nil {
		log.Fatalln(err)
		return
	}
	var bS = xray.NewSimpleBasicSetting("测试漏洞扫描任务", fmt.Sprintf("X-Ray更新扫描任务[%s]并执行，目标：%s", plan.Name, ip), []string{ip}, []string{ei})
	var tS = plan.TaskSetting
	var pO = xray.PlanUpdateOption{
		ID:           planId,
		BasicSetting: bS,
		TaskSetting:  tS,
		ExecRightNow: true,
	}
	err = plan.Update(pO)
	if err != nil {
		log.Fatalln(err)
		return
	}
	time.Sleep(5 * time.Second)
	plan, err = client.Plan(planId)
	if err != nil {
		log.Fatalln(err)
		return
	}
	fmt.Printf("更新计划ID[%d]为名称：%s，并执行扫描任务ID：%d\n", plan.Id, plan.Name, plan.XProcess[0])
}
